from selenium import webdriver
from selenium.webdriver.common.by import By

import time
import pytest
from web.data import DataBase



class Openweb:


    @staticmethod
    @pytest.fixture(params=[webdriver.Chrome(), webdriver.Edge()])
    def web_drive(request):
        url = request.param
        url.get('http://localhost:90')
        url.maximize_window()
        time.sleep(1)
        yield url
        url.quit()




    def web_login(self, url, user, password):

        try:
            url.find_element(By.ID, 'username').send_keys(user)
            url.find_element(By.ID, 'password').send_keys(password)
            url.find_element(By.ID, 'login_btn').click()
            time.sleep(2)
            result1 = url.title
            return result1
        except Exception as e:
            print(e)

    def web_login1(self, url, user, password):

        try:
            url.find_element(By.ID, 'username').send_keys(user)
            url.find_element(By.ID, 'password').send_keys(password)
            url.find_element(By.ID, 'login_btn').click()
            time.sleep(2)
            return url
        except Exception as e:
            print(e)


    def web_register(self, url, user, password1, password2):

        try:
            url.find_element(By.LINK_TEXT, '还没有账号？去注册').click()
            url.find_element(By.ID, 'username').send_keys(user)
            url.find_element(By.ID, 'password').send_keys(password1)
            url.find_element(By.ID, 'repassword').send_keys(password2)
            time.sleep(2)
            url.find_element(By.ID, 'login_btn').click()
            time.sleep(3)
            result1 = url.title

            sql = 'delete from user where username = %s and password = %s'
            param = (user, password1)
            DataBase().update(sql, param)
            return result1
        except Exception as e:
            print(e)



    def web_bank(self,url1):

        try:
            url = self.web_login1(url1, 'lisi', '123456')
            url.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
            time.sleep(1)
            url.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
            time.sleep(1)
            return url
        except Exception as e:
            print(e)

    def sum_div(self,url):

        try:
            bank_div = url.find_element(By.XPATH, '/html/body/div/div/main/div/div').find_elements(By.XPATH, './div')
            len1 = len(bank_div)
            return len1
        except Exception as e:
            print(e)


    def web_bank_insert(self, url1, address, type, number):

        try:
            l1 = []
            url = self.web_bank(url1)
            len1 = self.sum_div(url)
            l1.append(len1)

            url.find_element(By.ID, 'bankCard_add_modal_btn').click()
            time.sleep(1)
            url.find_element(By.ID, 'cardBank_add_input').send_keys(address)
            if type == '借记卡':
                url.find_element(By.XPATH,
                                     '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]/span').click()
            else:
                url.find_element(By.XPATH,
                                     '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span').click()
            url.find_element(By.ID, 'cardNum_add_input').send_keys(number)
            url.find_element(By.ID, 'bankCard_save_btn').click()
            time.sleep(3)

            len2 = self.sum_div(url)
            l1.append(len2)
            return l1
        except Exception as e:
            print(e)

    def web_bank_delete(self, url1, result):

        try:
            l1 = []
            url = self.web_bank(url1)
            len1 = self.sum_div(url)
            l1.append(len1)
            url.find_element(By.XPATH,
                             '/html/body/div/div/main/div/div/div[5]/div/div/div[1]/ul/li/button[2]').click()
            time.sleep(1)
            if result == '删除成功':
                url.find_element(By.XPATH,
                                 '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
            else:
                url.find_element(By.XPATH,
                                 '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]').click()
            time.sleep(1)
            len2 = self.sum_div(url)
            l1.append(len2)
            return l1
        except Exception as e:
            print(e)

    def web_bank_update(self, url1, address, type, number):

        try:

            url = self.web_bank(url1)

            url.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[1]').click()
            time.sleep(1)
            url.find_element(By.ID, 'cardBank_update_input').clear()
            url.find_element(By.ID, 'cardNum_update_input').clear()
            url.find_element(By.ID, 'cardBank_update_input').send_keys(address)
            if type == '借记卡':
                url.find_element(By.XPATH,
                                     '//*[@id="bankCardUpdateModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]/span').click()
            else:
                url.find_element(By.XPATH,
                                     '//*[@id="bankCardUpdateModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span').click()
            url.find_element(By.ID, 'cardNum_update_input').send_keys(number)
            url.find_element(By.ID, 'bankCard_update_btn').click()
            time.sleep(3)
            text1 = url.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[3]/div/div/div[1]/div/h4').text
            text2 = url.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[3]/div/div/div[2]/h4[1]').text
            if text1 == address+' ('+type+')' or text2 == '卡号：'+number:
                return '修改成功'
            else:
                return '修改失败'

        except Exception as e:
            print(e)

    def web_money(self, url, mima):

        try:
            url.find_element(By.XPATH, '/html/body/div[3]/div[2]/input').send_keys(mima)
            url.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
            time.sleep(3)
            text1 = url.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

            return text1
        except Exception as e:
            print(e)

    def web_money_self(self, url1, user, password, mima):

        try:
            url = self.web_login1(url1, user, password)
            url.find_element(By.XPATH, '/html/body/div[1]/div/aside/div[2]/nav/ul/li[2]/a').click()
            time.sleep(3)
            url.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
            time.sleep(3)
            url.find_element(By.XPATH,
                             '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
            time.sleep(3)
            text1 = self.web_money(url, mima)
            time.sleep(3)
            url.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button').click()
            return text1
        except Exception as e:
            print(e)


    def web_money_time(self, url1, user, password, mima):

        try:
            url = self.web_login1(url1, user, password)
            url.find_element(By.XPATH, '/html/body/div[1]/div/aside/div[2]/nav/ul/li[2]/a').click()
            time.sleep(2)
            url.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a').click()
            time.sleep(2)
            url.find_element(By.XPATH,
                             '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
            time.sleep(2)
            text1 = self.web_money(url, mima)
            return text1
        except Exception as e:
            print(e)

    def web_money_ji(self, url1, user, password, mima):

        try:
            url = self.web_login1(url1, user, password)
            url.find_element(By.XPATH, '/html/body/div[1]/div/aside/div[2]/nav/ul/li[2]/a').click()
            time.sleep(2)
            url.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
            time.sleep(2)
            url.find_element(By.XPATH,
                             '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[3]/td[9]/button').click()
            text1 = self.web_money(url, mima)
            return text1
        except Exception as e:
            print(e)


    def web_loan_apply(self, url1, user, password, money, day, result):

        try:
            url = self.web_login1(url1, user, password)
            url.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/a').click()
            time.sleep(2)
            url.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
            time.sleep(2)
            url.find_element(By.ID, 'amount').send_keys(money)
            url.find_element(By.ID, 'term').send_keys(day)
            url.find_element(By.ID, 'submit').click()
            time.sleep(2)
            url.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
            time.sleep(5)
            if '申请失败' in result:
                text1 = url.find_element(By.XPATH,
                                         '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text
                return text1
            else:
                return '申请成功!'

        except Exception as e:
            print(e)


    def web_loan_approve(self,url1, user, password, money, day):

        try:
            sr = self.web_loan_apply(url1, user, password, money, day, '申请成功!')
            url1.find_element(By.XPATH, '/html/body/div/div/header/nav/div/ul/li[1]/a').click()
            time.sleep(1)
            url1.find_element(By.XPATH, '/html/body/div/div/header/nav/div/ul/li[1]/ul/li[4]/a').click()
            url = self.web_login1(url1, 'admin', '123456')
            url.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[5]/a').click()
            time.sleep(1)
            url.find_element(By.XPATH, '/html/body/div/div/aside/div[2]/nav/ul/li[5]/ul/li[1]/a').click()
            time.sleep(1)
            n = int(str(url.find_element(By.XPATH,
                                         '/html/body/div/div/main/div/div/div/div/div[3]/div[1]/div/button[6]/span').text)[
                        0])
            url.find_element(By.XPATH,
                             '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[7]/button[1]').click()
            time.sleep(1)
            url.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
            time.sleep(1)
            n1 = int(str(url.find_element(By.XPATH,
                                          '/html/body/div/div/main/div/div/div/div/div[3]/div[1]/div/button[6]/span').text)[
                         0])
            x = False
            if n1 < n:
                x = True
            return x
        except Exception as e:
            print(e)

